package com.web.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kennycason.kumo.WordFrequency;
import com.kennycason.kumo.nlp.FrequencyAnalyzer;
import com.kennycason.kumo.nlp.tokenizer.WordTokenizer;
import com.kennycason.kumo.nlp.tokenizers.ChineseWordTokenizer;
import com.web.common.Dto.BrandInfoDto;
import com.web.common.enums.AppHttpCodeEnum;
import com.web.common.pojos.Car;
import com.web.common.responseResult.ResponseResult;
import com.web.common.vo.BrandInfoVo;
import com.web.mapper.CarMapper;
import com.web.service.Task3Service;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.*;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Lenovo]
 * @version : [v1.0]
 * @createTime : [2024/6/9 下午3:23]
 */
@Service
@Slf4j
public class Task3ServiceImpl extends ServiceImpl<CarMapper, Car> implements Task3Service {
    @Autowired
    private CarMapper carMapper;
    @Override
    public List<String> getTop10brand() {
        List<String> top10brandNameList = carMapper.getTop10brand();
        return top10brandNameList;
    }

    @Override
    public ResponseResult getBrandInfo(BrandInfoDto brandInfoDto) {
        List<Integer>  carSaleCounts = new ArrayList<>();
        List<Integer>  provinceSaleCounts = new ArrayList<>();
        int start,end;
        String[] starts = brandInfoDto.getDateStart().split("-");
        start = Integer.parseInt(starts[0])*100+Integer.parseInt(starts[1]);
        String[] ends = brandInfoDto.getDateEnd().split("-");
        end = Integer.parseInt(ends[0])*100+Integer.parseInt(ends[1]);

        if(brandInfoDto.getBrandName().isEmpty())
        {
            return ResponseResult.errorResult(AppHttpCodeEnum.FAILE,"缺失品牌名");
        }
        if (brandInfoDto.getProvince().equals("全国"))
        {
            brandInfoDto.setProvince("");
        }
        List<String> carNames = carMapper.getCarNameByBrand(start,end,brandInfoDto.getBrandName(),brandInfoDto.getProvince());
        for (String carName : carNames) {
            int carSaleCount = carMapper.getCarCountsByProvince(start,end,carName,brandInfoDto.getProvince());
            carSaleCounts.add(carSaleCount);
        }
        List<String> provinceNames = carMapper.getBrandNameByBrand(start,end,brandInfoDto.getBrandName());
        for (String provinceName : provinceNames) {
            int brandSaleCount = carMapper.getBrandCountsByProvince(start,end,provinceName,brandInfoDto.getBrandName());
            provinceSaleCounts.add(brandSaleCount);
        }
        provinceNames.addAll(List.of(new String[]{"台湾", "香港", "澳门"}));
        provinceSaleCounts.add(0);
        provinceSaleCounts.add(0);
        provinceSaleCounts.add(0);
        BrandInfoVo brandInfoVo = new BrandInfoVo();
        brandInfoVo.setCarNames(carNames);
        brandInfoVo.setCarSaleCounts(carSaleCounts);
        brandInfoVo.setProvinceNames(provinceNames);
        brandInfoVo.setProvinceSaleCounts(provinceSaleCounts);
        log.info("brandInfoVo:{}",brandInfoVo);
        return ResponseResult.okResult(brandInfoVo);
    }

    @Override
    public ResponseResult<List<Map<String, Integer>>> getWordCloud(String name) {
        String filePath = "src/main/java/com/web/text_files/"+name+".csv"; // 替换为你的CSV文件路径
        List<Map<String, Integer>> parsedData = parseCsvFile(filePath);
        return ResponseResult.okResult(parsedData);
    }
    public List<Map<String, Integer>> parseCsvFile(String filePath) {
        List<Map<String, Integer>> resultList = new ArrayList<>();

        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                if (line.equals("评论,出现次数"))
                {
                    continue;
                }
                String[] parts = line.split(",");
                if (parts.length == 2) {
                    String comment = parts[0];
                    int count = Integer.parseInt(parts[1]);
                    // 创建Map并添加到结果列表中
                    Map<String, Integer> commentCountMap = new HashMap<>();
                    commentCountMap.put(comment, count);
                    resultList.add(commentCountMap);
                } else {
                    System.out.println("警告：发现格式不正确的行，已跳过。");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            // 处理异常，例如返回null或抛出自定义异常
        }
        return resultList;
    }
}
